/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.d2g.realestate.persistence;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author d2g
 */
@Entity
@Table(name = "announce", catalog = "dd111", schema = "")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Announce.findAll", query = "SELECT a FROM Announce a"),
    @NamedQuery(name = "Announce.findByIdAnnounce", query = "SELECT a FROM Announce a WHERE a.idAnnounce = :idAnnounce"),
    @NamedQuery(name = "Announce.findByStartDate", query = "SELECT a FROM Announce a WHERE a.startDate = :startDate"),
    @NamedQuery(name = "Announce.findByFinishDate", query = "SELECT a FROM Announce a WHERE a.finishDate = :finishDate"),
    @NamedQuery(name = "Announce.findByType", query = "SELECT a FROM Announce a WHERE a.type = :type"),
    @NamedQuery(name = "Announce.findByStatus", query = "SELECT a FROM Announce a WHERE a.status = :status"),
    @NamedQuery(name = "Announce.findByDescription", query = "SELECT a FROM Announce a WHERE a.description = :description"),
    @NamedQuery(name = "Announce.findByOwnerAndCriterias", query = "SELECT a FROM Announce a WHERE "
    + "(a.idProperty.ownerUsername.username = :ownerUsername) and "
    + "(a.idProperty.address = :address or :address = '' or :address IS NULL) and "
    + "(a.idProperty.postcode = :postcode or :postcode = '' or :postcode IS NULL) and "
    + "(a.idProperty.numberBathroom >= :minNumberBathroom or :minNumberBathroom IS NULL) and "
    + "(a.idProperty.numberBathroom <= :maxNumberBathroom or :maxNumberBathroom IS NULL) and "
    + "(a.idProperty.numberBedroom >= :minNumberBedroom or :minNumberBedroom IS NULL) and "
    + "(a.idProperty.numberBedroom <= :maxNumberBedroom or :maxNumberBedroom IS NULL) and "
    + "(a.idProperty.furniture = :furnished or :furnished IS NULL) and "
    + "(a.type = :type or :type = '' or :type IS NULL) and "
    + "(a.idProperty.garden = :garden or :garden IS NULL) and "
    + "(a.price >= :minPrice or :minPrice  IS NULL) and "
    + "(a.price <= :maxPrice or :maxPrice  IS NULL) "
    + ""),
    @NamedQuery(name = "Announce.findByCriterias", query = "SELECT a FROM Announce a WHERE "
    + "(a.idProperty.address = :address or :address = '' or :address IS NULL) and "
    + "(a.idProperty.postcode = :postcode or :postcode = '' or :postcode IS NULL) and "
    + "(a.idProperty.numberBathroom >= :minNumberBathroom or :minNumberBathroom IS NULL) and "
    + "(a.idProperty.numberBathroom <= :maxNumberBathroom or :maxNumberBathroom IS NULL) and "
    + "(a.idProperty.numberBedroom >= :minNumberBedroom or :minNumberBedroom IS NULL) and "
    + "(a.idProperty.numberBedroom <= :maxNumberBedroom or :maxNumberBedroom IS NULL) and "
    + "(a.idProperty.furniture = :furnished or :furnished IS NULL) and "
    + "(a.type = :type or :type = '' or :type IS NULL) and "
    + "(a.idProperty.garden = :garden or :garden IS NULL) and "
    + "(a.price >= :minPrice or :minPrice  IS NULL) and "
    + "(a.price <= :maxPrice or :maxPrice  IS NULL) "
    + ""),
    @NamedQuery(name = "Announce.findByPrice", query = "SELECT a FROM Announce a WHERE a.price = :price")})
public class Announce implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Basic(optional = false)
    //@NotNull
    @Column(name = "id_announce", nullable = false)
    private Long idAnnounce;
    @Basic(optional = false)
    @NotNull
    @Column(name = "start_date", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date startDate;
    @Column(name = "finish_date")
    @Temporal(TemporalType.TIMESTAMP)
    private Date finishDate;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "type", nullable = false, length = 45)
    private String type;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "status", nullable = false, length = 45)
    private String status;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 10000)
    @Column(name = "description", nullable = false, length = 10000)
    private String description;
    @Basic(optional = false)
    @NotNull
    @Column(name = "price", nullable = false)
    private long price;
    @JoinColumn(name = "id_property", referencedColumnName = "id_property", nullable = false)
    @ManyToOne(optional = false)
    private Property idProperty;
    @JoinColumn(name = "winning_id_offer", referencedColumnName = "id_offer")
    @ManyToOne
    private Offer winningIdOffer;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idAnnounce")
    private Collection<Offer> offerCollection;

    public Announce() {
    }

    public Announce(Long idAnnounce) {
        this.idAnnounce = idAnnounce;
    }

    public Announce(Long idAnnounce, Date startDate, String type, String status, String description, long price) {
        this.idAnnounce = idAnnounce;
        this.startDate = startDate;
        this.type = type;
        this.status = status;
        this.description = description;
        this.price = price;
    }

    public Long getIdAnnounce() {
        return idAnnounce;
    }

    public void setIdAnnounce(Long idAnnounce) {
        this.idAnnounce = idAnnounce;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getFinishDate() {
        return finishDate;
    }

    public void setFinishDate(Date finishDate) {
        this.finishDate = finishDate;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public long getPrice() {
        return price;
    }

    public void setPrice(long price) {
        this.price = price;
    }

    public Property getIdProperty() {
        return idProperty;
    }

    public void setIdProperty(Property idProperty) {
        this.idProperty = idProperty;
    }

    public Offer getWinningIdOffer() {
        return winningIdOffer;
    }

    public void setWinningIdOffer(Offer winningIdOffer) {
        this.winningIdOffer = winningIdOffer;
    }

    @XmlTransient
    public Collection<Offer> getOfferCollection() {
        return offerCollection;
    }

    public void setOfferCollection(Collection<Offer> offerCollection) {
        this.offerCollection = offerCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idAnnounce != null ? idAnnounce.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Announce)) {
            return false;
        }
        Announce other = (Announce) object;
        if ((this.idAnnounce == null && other.idAnnounce != null) || (this.idAnnounce != null && !this.idAnnounce.equals(other.idAnnounce))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "" + ((description.length() > 20) ? this.description.substring(0, 20) : this.description);
    }
}
